<?php

require_once 'Framework/Controller/Action.php';

/**
 * Поиск
 *
 */
class SearchController extends Framework_Controller_Action
{
    const RESULT = 10;
    
    public function init()
    {
        // Определяем кто пришел (гость|левый чел)
        if ($this->session->get('guest_logged_in')) {
            $this->view->guest = 1; 
        } else if (!$this->session->get('logged_in')) {
            $this->view->not_auth = 1;
        }
        if ($this->session->get('user_id'))
        {
        	$this->_user = new User_Self($this->session->get('user_id'));
        	$this->view->messages_count = $this->_user->getNewMessagesCount();
        }
    }
    
    /**
     * Поисковая форма
     *
     */
    public function indexAction()
    {
        if ($this->getRequest()->getQuery('save')) {
            $this->view->search = $this->session->get('search');
        }
        
        $this->render();
    }
    
    /**
     * Результаты поиска
     *
     */
    public function resultAction()
    {
        $search = get_value($this->getRequest()->getQuery('search'));
        if (!is_array($search)) { // Ничего не ищем? Валим отсюда.
            $this->_redirect('/search/');
        }
        $page = $this->_getParam(0, 0);
        $result = User_Another::search($search, $page, self::RESULT, $paging);

        $this->session->set('search', $search);
        $this->view->paging = $paging;
        $this->view->page   = $page;
        $this->view->query  = $this->getRequest()->getServer('QUERY_STRING');
        $this->view->result = $result;
        
        $this->render();
    }
}